home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14785 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: news-relay.eworld.com!zdc!zippo!drn
  2. From: Clarence Chiang
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Template constructor problem
  5. Date: 1 Apr 1996 12:09:56 -0800
  6. Organization: Zippo
  7. Sender: http@doc.zippo.com
  8. Message-ID: <4jpd6k$2nu@doc.zippo.com>
  9. NNTP-Posting-Host: doorstop.spiderisland.com
  10.  
  11. In article <4jjtgf$ebo@csugrad.cs.vt.edu>, rsuri@csugrad.cs.vt.edu says...
  12. >
  13. >Ok, take this code using g++ v2.7
  14. >
  15. >queue.h:
  16. >
  17. >template <class QueueItem> class Queue {
  18. > private:
  19. >  QueueItem buffer[100];
  20. >  int head, tail, count;
  21. >
  22. > public:
  23. >  Queue();
  24. >  void Insert (QueueItem item);
  25. >  QueueItem Remove();
  26. >  int Size();
  27. >  ~Queue();
  28. >};
  29. >
  30. >template <class QueueItem>
  31. >Queue<QueueItem>::Queue() : count(0), head(0), tail(0) {}
  32. >
  33. >.....[the rest of the mothods]
  34. >
  35. >
  36. >main.cc:
  37. >
  38. >#include<stdio.h>
  39. >#include "queue.h"
  40. >
  41. >Queue<int> myqueue;
  42. >
  43. >main () {
  44. >
  45. >}
  46. >
  47. >
  48. >Compiler error when linking:
  49. >collect2: ld returned 1 exit status
  50. >/usr/lib/cmplrs/cc/ld:
  51. >Undefined:
  52. >Queue<int>::Queue(void)
  53. >
  54. >
  55. >If I don't declare a queue object, then I don't get the linker error.  
  56. >Any suggestion??  Thanks in advance.
  57. >
  58. >-Raj
  59. >
  60.  
  61. The compiler is correct. According to the C++ language specification, if you
  62. declare a ctor that takes parameter(s). You must also declare one that doesn't
  63. take parameter (i.e. Queue<QueueItem>::Queue(void) ). Obviously g++ is expecting
  64. there is one. The compiler shouldn't wait til link time to issue an error, it
  65. should check for it and give out a compile-time error.
  66.  
  67. Clarence Chiang
  68. Spider Island Software
  69.